package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/8.
 */
public class SearchAnchorTask extends CouponHttpBaseTask {
    private int mSize;
    private int mPageNo;
    private String mKeyword;
    private static final String SEARCHVIDEOJSON = "searchVideoList";


    public SearchAnchorTask(String keyword, int size, int pageNo) {
        super(TaskType.SEARCHANCHOR_TASK);
        mSize = size;
        mPageNo = pageNo;
        mKeyword = keyword;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject respJson = response.getBody();
        int pageCount = 0;
        int currentPage = 0;
        int nextPage = 0;
        List<EatShowUserInfo> list = new ArrayList<EatShowUserInfo>();
//        if (respJson != null) {
//            JSONObject jsonString = respJson.getJSONObject(SEARCHVIDEOJSON);
            currentPage = respJson.getIntValue(ProtocolConst.CURRENT_PAGE);
            pageCount = respJson.getIntValue(ProtocolConst.PAGE_COUNT);
            nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
            list = JSON.parseArray(respJson.getString(ProtocolConst.LIST_TAG), EatShowUserInfo.class);
//        }

        notifyTaskCompleted(nextPage, list);

    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT) {
            CouponHttpRequest request = new CouponHttpRequest(RequestType.SEARCHANCHOR_REQUEST);
            JSONObject json = new JSONObject();
            json.put("size", mSize);
            json.put("pageNo", mPageNo);
            json.put("keyword", mKeyword);
            request.setData(json.toString());
            request.setApiNo(ApiNo.SEACHANCHOR);
            addRequest(request);
        }
    }
}
